package com.hp.goods_service.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hp.common_api.entity.Sku;
import com.hp.goods_service.config.RabbitMQConfig;
import com.hp.goods_service.service.ISkuService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * SKU商品的控制器
 */
@RestController
public class SkuController {

    @Autowired
    private ISkuService skuService;

    /**
     * 通过SpuID查询下面所有的SKU
     * @return
     */
    @GetMapping("/skus/spu/{spuId}")
    public ResponseEntity<List<Sku>> getSkusBySpuId(
            @PathVariable Long spuId){
        List<Sku> skus = skuService.list(new QueryWrapper<Sku>().lambda().eq(Sku::getSpuId, spuId));
        return ResponseEntity.ok(skus);
    }

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @GetMapping("/testMQ")
    public Long testMQ(Long id){
        rabbitTemplate.convertAndSend(
                RabbitMQConfig.GOODS_EXCHANGE,
                RabbitMQConfig.KEY_GOODS_SAVE,
                id);
        return id;
    }
}
